﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712518_0712510_LTGame
{
    public class Hero : Character
    {
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame = 50;
        KeyboardState m_Keyboard;
        int frameStandsinceLastFrame = 0;
        int millisecondsPerFrameStand = 500;
        
        public Hero()
        {
            currentState = new Sprite();
            faceDirection = 0;
            movement = new List<Animation>();
            behavior = State.FALLING;
            oldState = behavior;
        }
        
        
        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);
            position = new Vector2(25,600);
            pos2 = new Vector2(0, 0);
           // movement = new Animation[1];
            speed = 2;
            initSkill(@"Hero\run.txt", Content, @"imagesZero/zerorun_edit");
            unLock("RUN");
            initSkill(@"Hero\stand.txt", Content, @"imagesZero/zerostand");
            unLock("STAND");
            initSkill(@"Hero\jump.txt", Content, @"imagesZero/zerojump");
            unLock("JUMP");
            initSkill(@"Hero\fall.txt", Content, @"imagesZero/zerojump");
            unLock("FALL");
            initSkill(@"Hero\float.txt", Content, @"imagesZero/zerojump");
            unLock("FLOAT");
            initSkill(@"Hero\land.txt", Content, @"imagesZero/zerojump");
            unLock("LAND");
           
        }
        public override void Update(GameTime gameTime)
        {
            UpdateTransition(gameTime);
            UpdateInput(gameTime);       
            UpdateMovement(gameTime);
            updateGameTime(gameTime);


        }
        public void updateGameTime(GameTime gameTime)
        {
            for (int i = 0; i < movement.Count; i++)
            {
                movement[i].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            }
        }
        public void UpdateMovement(GameTime gameTime)
        {
           
                switch (behavior)
                {
                    case State.STANDING:
                        {
                            
                            oldState = State.STANDING;
                            currentMove = SetCurrentMove("STAND");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("STAND");
                                }

                            }
                            break;
                        }
                    case State.RUNNING:
                        {
                          
                            oldState = State.RUNNING;
                            currentMove = SetCurrentMove("RUN");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("RUN");
                                }

                            }
                            break;
                        }
                    case State.FALLING:
                        {
                            
                            oldState = State.FALLING;
                            currentMove = SetCurrentMove("FALL");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("FALL");
                                }

                            }
                            break;
                        }
                    case State.JUMPING:
                        {
                          
                            oldState = State.JUMPING;
                            currentMove = SetCurrentMove("JUMP");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("JUMP");
                                }

                            }
                            break;
                        }
                    case State.FLOATING:
                        {
                            
                            oldState = State.FLOATING;
                            currentMove = SetCurrentMove("FLOAT");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("FLOAT");
                                }

                            }
                            break;
                        }
                    case State.LANDING:
                        {
                          
                            oldState = State.LANDING;
                            currentMove = SetCurrentMove("LAND");
                            oldMove = currentMove;
                            if (currentMove != -1)
                            {
                                movement[currentMove].timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                                if (movement[currentMove].timeSinceLastFrame > movement[currentMove].timePerFrame)
                                {
                                    movement[currentMove].timeSinceLastFrame -= movement[currentMove].timePerFrame;
                                    SetAnimation("LAND");
                                }

                            }
                            break;
                        }
                    default:
                        {
                            //    if (onGround == 0)
                            //    {
                            //        behavior = State.FALLING;
                            //    }
                            //    if (onGround == 1)
                            //    {
                            //        behavior = State.STANDING;
                            //    }
                            break;
                        }
                }
            
        }
        public void UpdateTransition(GameTime gameTime)
        {
            if (onGround == 0)
            {
                if (behavior == State.JUMPING && pos2.Y >= 0)
                {
                    oldMove = currentMove;
                            behavior = State.FLOATING;               
                }
                else
                {
                    if (behavior == State.FLOATING)
                    {
                        if (movement[currentMove].canCancel == -1 && movement[currentMove].currentFrame != movement[currentMove].spriteCount - 1)
                        {
                            //     currentMove = oldMove;
                            behavior = oldState;

                        }
                        else
                        {

                            oldMove = currentMove;
                           behavior = State.FALLING;
                            oldState = behavior;
                        }
                       
                    }
                }
            }
            else
            {
                if (behavior == State.FALLING)
                {
                    oldMove = currentMove;
                            behavior = State.LANDING;
                 
                }
                else
                {
                    if (behavior == State.LANDING)
                    {
                        if (movement[currentMove].canCancel == -1 && movement[currentMove].currentFrame != movement[currentMove].spriteCount - 1)
                        {
                            //     currentMove = oldMove;
                            behavior = oldState;

                        }
                        else
                        {
                            oldMove = currentMove;
                   //         behavior = State.STANDING;
                            oldState = behavior;
                        }
                       
                    }
                }
            }
        }
        public void UpdateInput(GameTime gameTime)
        {
         
           
                pos2.Y += 0.8f;
                pos2.X *= 0.6f;
                m_Keyboard = Keyboard.GetState();
                if (m_Keyboard.GetPressedKeys().Length != 0)
                {

                    if (m_Keyboard.IsKeyDown(Keys.Right))
                    {
                        faceDirection = 1;
                        pos2.X += speed;
                        if (onGround == 1)
                        {

                            behavior = State.RUNNING;
                        }
                    }
                    if (m_Keyboard.IsKeyDown(Keys.Left))
                    {
                        faceDirection = 0;
                        pos2.X -= speed;
                        if (onGround == 1)
                        {

                            behavior = State.RUNNING;
                        }
                    }
                    if (m_Keyboard.IsKeyDown(Keys.Up))
                    {
                        //onGround = 0;
                        if (/*input == 0 &&*/ onGround == 1 && status == 0)
                        {
                            pos2.Y += -20;
                            onGround = 0;
                            status = 1;
                            // input = 1;
                            behavior = State.JUMPING;
                        }
                        //if (status == 1)
                        //{
                        //    SetAnimation("JUMP");
                        //}
                        //if (pos2.Y >= 0)
                        //{
                        //    status = 0;
                        //    SetAnimation("FALL");
                        //}

                    }
                    if (m_Keyboard.IsKeyDown(Keys.Down))
                    {

                    }
                }
                else
                {
                   
                   
                }

               
        }
       
        public void Draw(SpriteBatch spriteBatch)
        {
            
        }
    }
}
